package cs271.examples.binarytrees;

public class TreeTraversal {
	
	//Below we see preOrder and postOrder traversal 'strategies'
	//This is a practical implementation of the strategy pattern.
	
	public static  void preOrder(BinaryTreeElement e, NodeFound f) {
		if(e==null) return;
		preOrder(e.getLeft(), f);
		f.foundNode(e);
		preOrder(e.getRight(), f);
	}
	
	public static void postOrder(BinaryTreeElement e, NodeFound f) {
		if(e==null) return;
		postOrder(e.getRight(), f);
		f.foundNode(e);
		postOrder(e.getLeft(), f);
	}
	
	public static void main(String[] args) {
		BinaryTreeElement root = new BinaryTreeElement("root", null);
		root.setLeft(new BinaryTreeElement("left", root));
		root.setRight(new BinaryTreeElement("right", root));
		
		postOrder(root, new NodeFound() {
			public void foundNode(BinaryTreeElement e) {
				System.out.println(e.getElement());
			}
		});
	}
}
